package com.hk.aefz.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 解决跨域
 * 配置类
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsFilter corsFilter() {
//        1. 添加CorsCOnfig配置信息
        CorsConfiguration config = new CorsConfiguration();
//          2. 允许的域
        config.addAllowedOrigin("http://www.alphazhe.com:8081");
//        config.addAllowedOrigin("");
//        config.addAllowedOrigin("");
//          3. 是否发送Cookie信息
        config.setAllowCredentials(true);
//        4. 允许的请求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
//        5. 允许的头信息
        config.addAllowedHeader("*");
//        6. 添加映射路径 拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",config);
//        7. 返回新的CorsFilter
        return new CorsFilter(source);
    }

}
